<?php

namespace app\saas\service;

use app\common\logic\SmsLogic;
use app\common\model\commonModel;
use app\common\util\ErrorCode;
use app\saas\model\trSetting;
use think\facade\Validate;

/**
 * Class Auth
 * @package app\admin\service
 * 登录服务类
 */
class AttachService
{
    private $params;
    private $returnData;

    public function __construct($data, $params)
    {
        $this->returnData = $data;
        $this->params = $params;
    }

    public function doSendSms(){
        if(empty($this->params['mobile'])){
            return ErrorCode::code("params_not_exist", "手机号");
        }
        $settingData = trSetting::getFields("sms_id,sms_secret,sms_sign,sms_code");
        if(empty($settingData) || !$settingData["smsId"] || !$settingData["smsSecret"] || !$settingData["smsCode"]){
            return ErrorCode::code("customized", "短信未配置");
        }
        if(!Validate::isMobile($this->params["mobile"])){
            return ErrorCode::code("params_format", "mobile");
        }
        if(commonModel::getCount(smsRecordTable, [
            ["mall_id", '=', 0], ["mobile", "=", $this->params['mobile']], ["status", "=", 1], ["create_time", ">=", date("Y-m-d H:i:s", time() - 60)]
        ])){
            return ErrorCode::code("customized", "一分钟内请勿频繁获取验证码");
        }
        $code = rand(1000, 9999);
        if(SmsLogic::sendSms($settingData["smsCode"], [$code], $this->params['mobile'], "86", $msg, $settingData)){
            SmsLogic::recordLog($settingData["smsCode"], $this->params['mobile'], "发送验证码", $code, 1);
            $this->returnData["msg"] = "短信发送成功";
        }else{
            $this->returnData = ErrorCode::$errorCode["fail"];
            $this->returnData["msg"] = "短信发送失败，".$msg;
        }
        return $this->returnData;
    }

    public function getSettingInfo(){
        if(request()->manageData["type"] != "1"){
            return ErrorCode::code("customized", "您无权限操作");
        }
        $settingData = trSetting::getFields("*");
        if(empty($settingData)){
            echo "<pre>";print_r(trSetting::getTable());die("2");
        }
        echo "<pre>";print_r($settingData);die("2");
    }
}